package com.link.base.base.securitypage.service;

import com.link.base.base.securitypage.dao.mybatis.mapper.SecurityPageMapper;
import com.link.base.base.securitypage.model.SecurityPage;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.AppConstants;
import com.link.core.util.RedisConstants;
import com.link.core.util.RedisUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;


/**
 * @author: unknown
 * @date: unknown
 * @Comments 安全性菜单服务
 */
@Service
public class SecurityPageServiceImpl extends BasicServiceImpl<SecurityPage> implements SecurityPageService {

    @Resource
    private SecurityPageMapper securityPageMapper;

    @Override
    public BasicMapper<SecurityPage> getBasicMapper() {
        return securityPageMapper;
    }

    @Override
    public String[] getAllMenuPageURL(SecurityPage record) {
        return securityPageMapper.getAllMenuPageURL(record);
    }

    @Override
    public void loadMenuPageUrlToRedis(SecurityPage record) {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            if (record == null) {
                //000账套调用则刷新所有，其余则刷新000与当前账套的
                record = new SecurityPage();
                record.setLoginCorpId(AppConstants.ADMIN_CORP_ID);
            }
            String[] resultStr = securityPageMapper.getAllMenuPageURL(record);

            if (AppConstants.ADMIN_CORP_ID.equals(record.getLoginCorpId())) {
                //000账套调用则刷新所有，其余则刷新000与当前账套的
                jedis.del(RedisConstants.MENU_PAGE_URL_SET_RKEY);
            }
            if (resultStr != null && resultStr.length > 0) {
                jedis.sadd(RedisConstants.MENU_PAGE_URL_SET_RKEY, resultStr);
            }

        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    @Override
    public List<SecurityPage> getPageAndUrlByMenuId(SecurityPage record) {
        return securityPageMapper.getPageAndUrlByMenuId(record);
    }
}
